package ssi.staaar;

import ssi.staaar.control.StaaarControl;
import ssi.staaar.exceptions.ServerException;
import ssi.staar.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegistrationActivity extends Activity {
	private AlertDialog.Builder builder, builderServer, builderFields;
	
	private boolean checkEmail(String email) {
		if (email.length() < 1 || email.length() > 50)
			return false;
		if (!email.matches(".+@.+\\.[a-z]+"))
			return false;
		return true;
	}

	private boolean checkPasswords(String pwd, String confirm) {
		if (pwd.length() < 8 || pwd.length() > 20 || !pwd.equals(confirm))
			return false;
		return true;
	}

	private boolean checkUsername(String username) {
		if (username.length() < 1 || username.length() > 50)
			return false;
		return true;
	}

	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.registration);
	    
	    builder = new AlertDialog.Builder(this);
	    builder.setTitle(R.string.app_name)
	    	.setMessage(R.string.registration_field_not_valid)
		    .setNeutralButton("OK", new DialogInterface.OnClickListener() {
		        public void onClick(DialogInterface dialog, int id) {
		             dialog.dismiss();
		        }
		});
	    builderServer = new AlertDialog.Builder(this);
	    builderServer.setTitle(R.string.app_name)
	    	.setMessage(R.string.server_unreachable)
		    .setNeutralButton("OK", new DialogInterface.OnClickListener() {
		        public void onClick(DialogInterface dialog, int id) {
		             dialog.dismiss();
		        }
		});
	    builderFields = new AlertDialog.Builder(this);
	    builderFields.setTitle(R.string.app_name)
	    	.setMessage(R.string.missing_fields)
		    .setNeutralButton("OK", new DialogInterface.OnClickListener() {
		        public void onClick(DialogInterface dialog, int id) {
		             dialog.dismiss();
		        }
		});
	    final Button sendButton = (Button)findViewById(R.id.sendRegData);
	    sendButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				final EditText username = (EditText)findViewById(R.id.usernameEdit);
				final EditText email = (EditText)findViewById(R.id.emailEdit);
				final EditText password = (EditText)findViewById(R.id.pwdEdit);
				final EditText passwordConfirm = (EditText)findViewById(R.id.pwdConfirmEdit);
				if (username.getText().toString().equals("") || 
						email.getText().toString().equals("")) {
					AlertDialog missingFields = builderFields.create();
					missingFields.show();
				} else {
					if (checkPasswords(password.getText().toString(), 
							passwordConfirm.getText().toString()) && 
							checkEmail(email.getText().toString()) &&
							checkUsername(username.getText().toString())) 
					{
						StaaarControl stControl = new StaaarControl(
								getApplicationContext(),
								getString(R.string.serverAddress), 
								Integer.parseInt(getString(R.string.serverPort)),
								getString(R.string.keyPassword)
								);
						try {
							stControl.startConnection();
							boolean reg = stControl.registrate(username.getText().toString(), 
									email.getText().toString(), 
									password.getText().toString());
							stControl.finishConnection();
							if (reg) {
								SharedPreferences settings = getSharedPreferences(Staaar.PREFS_NAME, 0);
								SharedPreferences.Editor editor = settings.edit();
								editor.putString("Username", username.getText().toString());
								editor.putBoolean("Registered", true);
								editor.commit();
								setResult(RESULT_OK);
								finish();
							}
							else 
								setResult(RESULT_CANCELED);
						} catch(ServerException e) {
							AlertDialog alertServer = builderServer.create();
							alertServer.show();
							e.printStackTrace();
						}
					} else {
						AlertDialog alert = builder.create();
						alert.show();
					}
				}
			}
		});
	}
}
